package com.spring.demo.spring.core.proxy;

import cn.hutool.json.JSONUtil;
import com.spring.demo.springdemo.PurchaseOrderService;
import org.springframework.aop.AfterAdvice;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.framework.ProxyFactory;

import java.lang.reflect.Method;

/**
 * @Author RenPu
 * @Date 2024/2/18 19:54
 * @Version 1.0
 * @Description:
 **/
public class SpringProxy {

    public static void main(String[] args) {

        //Spring内部自己的代理实现工厂
        ProxyFactory proxyFactory = new ProxyFactory();

        PurchaseOrderService purchaseOrderService = new PurchaseOrderService();

        proxyFactory.setTarget(purchaseOrderService);

        //根据此属性设置，用来区分使用JDK代理还是CGLIB代理来实现
//        proxyFactory.setInterfaces();
        proxyFactory.addAdvice(new MethodBeforeAdvice() {
            @Override
            public void before(Method method, Object[] args, Object target) throws Throwable {
                System.out.println("B E F O R E =================");
            }
        });

        PurchaseOrderService proxy =(PurchaseOrderService) proxyFactory.getProxy();
        proxy.helloOne();

    }
}
